package com.foocus.foocuscloud.provider.log.service;

import com.foocus.foocuscloud.common.aop.db.SwitchDB;
import com.foocus.foocuscloud.common.exception.BusinessException;
import com.foocus.foocuscloud.common.utils.UUIDUtil;
import com.foocus.foocuscloud.provider.log.dao.LogWrapperDao;
import com.foocus.foocuscloud.provider.log.domain.BasTaskLog;
import com.foocus.foocuscloud.provider.log.domain.LogRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class LogWrapperService {

    private static final Logger LOGGER = LoggerFactory.getLogger(LogWrapperService.class);

    @Resource
    private LogWrapperDao logWrapperDao;

    @SwitchDB("import")
    public LogRecord getLogWrapperService(String id) {
        LogRecord logWrapper = logWrapperDao.getLogWrapper(id);
        if (logWrapper == null) {
            throw new BusinessException("日志实体为空");
        }

        return logWrapper;
    }

    public LogRecord getLogWrapper2Service(String id) {
        LogRecord logWrapper = logWrapperDao.getLogWrapper(id);
        if (logWrapper == null) {
            throw new BusinessException("日志实体为空");
        }

        return logWrapper;
    }

    @SwitchDB("oracle")
    public BasTaskLog getLogWrapper3Service(String id) {
        BasTaskLog basTaskLog = logWrapperDao.getBasTaskLog(id);
        if (basTaskLog == null) {
            throw new BusinessException("日志实体为空");
        }

        return basTaskLog;
    }

    @Transactional(rollbackFor = Exception.class)
    public void addLogWrapper(LogRecord logRecord) {

        logRecord.setId(UUIDUtil.uuid());
        logRecord.setOperateType("INSERT");
        logWrapperDao.addLogWrapper(logRecord);
        LOGGER.info("写入业务日志进库");
    }
}
